/*
 * FreeModbus Libary: LPC214X Port
 * Copyright (C) 2007 Tiago Prado Lone <tiago@maxwellbohr.com.br>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * File: $Id: porttimer.c,v 1.1 2007/04/24 23:15:18 wolti Exp $
 */

/* ----------------------- Platform includes --------------------------------*/
#include <sys.h>
#include "fmbport.h"

/* ----------------------- Modbus includes ----------------------------------*/
#include "../mb.h"
#include "../mbport.h"

/* ----------------------- static functions ---------------------------------*/
static uint8_t bTimerOn;
static uint16_t TimerPv;
static uint16_t TimerSv;
/* ----------------------- Start implementation -----------------------------*/
BOOL
xMBPortTimersInit( USHORT usTim1Timerout50us )
{
    bTimerOn = 0;
    TimerSv = 1 + usTim1Timerout50us/20;
    TimerPv = 0;
    return TRUE;
}

/* ----------------------- timer on -----------------------------*/
void
vMBPortTimersEnable(  )
{
    bTimerOn = 1;
    TimerPv = 0;
}
/* ----------------------- timer off -----------------------------*/
void
vMBPortTimersDisable(  )
{
    bTimerOn = 0;
    TimerPv = 0;
}
/* ----------------------- timer ruan -----------------------------*/
void
prvvTIMERExpiredISR( void )
{
    if(bTimerOn){
        TimerPv++;    
        if(TimerPv >= TimerSv) {
            ( void )pxMBPortCBTimerExpired(  );
        }
    } else {
        TimerPv = 0;
    }
}
